package com.bitmapgame;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity implements SensorEventListener
{
    private BallRenderer view;
    private Sensor accelmeter;
    private SensorManager sm;
    private Button startButton, restartButton, mainMenuButton;
    private EditText bounceScoreET;

    private void RegisterAccelmeter()
    {
        sm.registerListener(this, accelmeter, SensorManager.SENSOR_DELAY_GAME);
    }
    private void UnregisterAccelmeter()
    {
        sm.unregisterListener(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        //Register accelerometer
        sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        accelmeter = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);

        //Register display
        view = new BallRenderer(this);
        setContentView(R.layout.main_menu);

        startButton = (Button) findViewById(R.id.startButton);
        startButton.setOnClickListener(startListener);
    }

    protected void onResume() {
        super.onResume();
        RegisterAccelmeter();
    }

    protected void onPause() {
        super.onPause();
        UnregisterAccelmeter();
    }

    private void DisplayFailScreen()
    {
        UnregisterAccelmeter();
        setContentView(R.layout.fail_screen);

        restartButton = (Button) findViewById(R.id.restartButton);
        restartButton.setOnClickListener(restartListener);
        mainMenuButton = (Button) findViewById(R.id.mainMenuButton);
        mainMenuButton.setOnClickListener(mainMenuListener);

        bounceScoreET = (EditText) findViewById(R.id.bounceScoreEditText);
        bounceScoreET.setText(Integer.toString(view.bounces));
    }

    private void StartGame()
    {
        setContentView(view);
        RegisterAccelmeter();
        view.initializationNeeded = true;
    }

    public View.OnClickListener restartListener = new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            StartGame();
        }
    };

    public View.OnClickListener mainMenuListener = new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            UnregisterAccelmeter();
            setContentView(R.layout.main_menu);
            startButton = (Button) findViewById(R.id.startButton);
            startButton.setOnClickListener(startListener);
        }
    };

    public View.OnClickListener startListener = new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            StartGame();
        }
    };

    @Override
    public void onSensorChanged(SensorEvent event)
    {
        if(view.failCondition)
        {
            DisplayFailScreen();
        }
        else
        {
            //Give BallRenderer values and make it run again
            view.SetXAccel(event.values[0]);
            view.invalidate();
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy)
    {

    }
}
